/**
 * 
 */
package com.kamengoranchev.musicrecognizer;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Activity after result set of HTTP post is received from server 
 * @author Kamen
 * 
 */
public class ResultsActivity extends Activity {
	private static final String TAG = "RESULTS_ACTIVITY";
	
	public static final String EXTRA_RESULTS = "com.kamengoranchev.musicrecognizer.EXTRA_RESULTS";

	private List<SongDescriptor> songs = new ArrayList<SongDescriptor>(); 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(getIntent() != null && getIntent().getExtras() != null){
			String resultsExtra = getIntent().getExtras().getString(EXTRA_RESULTS);
			parseResults(resultsExtra);
		}
		
		setContentView(R.layout.activity_results);
		
		final ListView listResults = (ListView) findViewById(R.id.lst_results);
		
		listResults.setAdapter(new BaseAdapter() {
			
			public View getView(int index, View converterView, ViewGroup root) {
//				LinearLayout layout = (LinearLayout) LayoutInflater.from(ResultsActivity.this).inflate(R.layout.list_item, root);
//				
//				final TextView songName = (TextView) layout.findViewById(R.id.txt_song_name);
//				final TextView artist = (TextView) layout.findViewById(R.id.txt_artist);
//				final TextView distance= (TextView) layout.findViewById(R.id.txt_distance);
//				
				SongDescriptor songDescriptor = songs.get(index);
//				
//				songName.setText(songDescriptor.getName());
//				artist.setText(songDescriptor.getArtist());
//				distance.setText(String.valueOf(songDescriptor.getDistance()));
				
				
				TextView layout = new TextView(ResultsActivity.this);
				layout.setText(String.format("%s - %s (%s)", songDescriptor.getName(), songDescriptor.getArtist(), String.valueOf(songDescriptor.getDistance())));
				return layout;
				
			}
			
			public long getItemId(int index) {
				return index;
			}
			
			public Object getItem(int index) {
				return songs.get(index);
			}
			
			public int getCount() {
				return songs.size();
			}
		});
	}
	
	private static final String JSON_PROPERTY_SONGNAME = "songname";
	private static final String JSON_PROPERTY_ARTIST = "artist";
	private static final String JSON_PROPERTY_DISTANCE = "distance";
	
	private void parseResults(String resultsJson){
		songs.clear();
		
		try {
			JSONArray resultsArray = new JSONArray(resultsJson);
			int resultsCount = resultsArray.length();
			for(int i = 0; i < resultsCount; i++){
				JSONObject songJson = resultsArray.getJSONObject(i);
				
				String songName = songJson.getString(JSON_PROPERTY_SONGNAME);
				String artist = songJson.getString(JSON_PROPERTY_ARTIST);
				double distance = songJson.getDouble(JSON_PROPERTY_DISTANCE);
				
				SongDescriptor songDescriptor = new SongDescriptor();
				songDescriptor.setName(songName);
				songDescriptor.setArtist(artist);
				songDescriptor.setDistance(distance);
				
				songs.add(songDescriptor);
			}
		
		} catch (JSONException e) {
			Log.e(TAG, "Problem while parsing the JSON response:" + e.getMessage());
		}
		
		
	}
}
